﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DTO;
using DAO;

namespace QuanLySach
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static Section_DTO Section { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DoLogin();
        }

        private void DoLogin()
        {
            Visibility = System.Windows.Visibility.Hidden;
            LoginDialog login = new LoginDialog();
            login.ShowDialog();
            Section = login.Section;
            if (Section == null)
            {
                this.Close();
                return;
            }
            ResetSection();
            UpdatePermision();
        }

        private void UpdatePermision()
        {
            ViTri_DTO vitri = Section.Permision;
            SetNhapSach(vitri.NhapSach);
            SetLapHoaDon(vitri.LapHoaDon);
            SetTraCuu(vitri.TraCuuSach);
            SetLapPhieuThuTien(vitri.LapPhieuThuTien);
            SetLapBaoCao(vitri.LapBaoCao);
            SetThayDoiQD(vitri.ThayDoiQuyDinh);
        }

        private void SetNhapSach(bool enable)
        {
            sortLapPhieu.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            sortLapBC.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            menuSach.IsEnabled = enable;
            menuTheLoai.IsEnabled = enable;
            menuTacGia.IsEnabled = enable;
            menuNhapSach.IsEnabled = enable;
            menuBCTon.IsEnabled = enable;
        }

        private void SetLapHoaDon(bool enable)
        {
            sortLapHoaDon.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            menuKhachHang.IsEnabled = enable;
            menuBanSach.IsEnabled = enable;
        }

        private void SetTraCuu(bool enable)
        {
            sortTraCuuSach.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            menuTraCuu.IsEnabled = enable;
        }

        private void SetLapPhieuThuTien(bool enable)
        {
            sortLapPhieuThu.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            sortLapBCNo.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            menuThuTien.IsEnabled = enable;
            menuBCNo.IsEnabled = enable;
        }

        private void SetLapBaoCao(bool enable)
        {
            //sortLapBC.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            menuBC.IsEnabled = enable;
        }

        private void SetThayDoiQD(bool enable)
        {
            sortThayDoiQD.Visibility = enable ? Visibility.Visible : Visibility.Collapsed;
            menuThayDoiQD.IsEnabled = enable;
            menuNhanVien.IsEnabled = enable;
        }

        private void DangXuatMenu_Click(object sender, RoutedEventArgs e)
        {
            Section = null;
            Visibility = System.Windows.Visibility.Hidden;
            DoLogin();
        }

        private void ResetSection()
        {
            MainContent.Children.Clear();
            MainContent.Children.Add(new WelcomeUserControl(Section));
            this.Visibility = System.Windows.Visibility.Visible;
        }

        private void menuThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void sortThoat_Click(object sender, RoutedEventArgs e)
        {
            Section = null;
            Visibility = System.Windows.Visibility.Hidden;
            DoLogin();
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            SQLConnectData.DataConnection.DisConnect();
        }

        /// <summary>
        /// LẬP PHIẾU NHẬP SÁCH
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sortLapPhieu_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new LapPhieuNhapSachUserControl(Section, this));
        }

        private void menuThuTien_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new LapPhieuThuUserControl(Section, this));
        }

        private void menuBCNo_Click(object sender, RoutedEventArgs e)
        {
            
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new BaoCaoCongNoUserControl(Section, this));
        }

        private void menuTKSach_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new TimKiemSachUserControl(Section, this));
        }

        private void menuBCTon_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new BaoCaoTonUserControl(Section, this));
        }

        private void sortLapHoaDon_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new LapHoaDonBanUserControl(Section, this));
        }

        private void ThayDoiMKMenu_Click(object sender, RoutedEventArgs e)
        {
            ThayDoiMatKhauDialog diag = new ThayDoiMatKhauDialog(Section);
            diag.ShowDialog();
        }

        public void ChangeUI(UIElement ele)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(ele);
        }

        private void sortTraCuuSach_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new TimKiemSachUserControl(Section, this));
        }

        private void sortThayDoiQD_Click(object sender, RoutedEventArgs e)
        {
            ThayDoiQuyDinhDialog diag = new ThayDoiQuyDinhDialog(Section);
            diag.ShowDialog();
        }

        private void sortLapPhieuThu_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new LapPhieuThuUserControl(Section, this));
        }

        private void sortLapBC_Click(object sender, RoutedEventArgs e)
        {
            // BÁo cáo tồn
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new BaoCaoTonUserControl(Section, this));
        }

        private void sortLapBCNo_Click(object sender, RoutedEventArgs e)
        {
            if (MainContent.Children.Count != 0)
                MainContent.Children.Clear();
            MainContent.Children.Add(new BaoCaoCongNoUserControl(Section, this));
        }
    }
}
